<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改用户</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery-2.1.0.min.js"></script>
    <!--<script src="js/bootstrap.min.js"></script>-->

</head>
<body>
<div class="container" style="width: 400px;">
    <h3 style="text-align: center;">修改用户</h3>
    <div class="form-group">
        <label for="username">用户名：</label>
        <input type="text" class="form-control" id="loginname" name="loginname" readonly="readonly"/>
    </div>

    <div class="form-group">
        <label for="username">昵称：</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="请输入昵称"/>
    </div>
    <div class="form-group">
        <label for="password">密码：</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"/>
    </div>

    <div class="form-group">
        <label for="password2">确认密码：</label>
        <input type="password" class="form-control" id="password2" name="password" placeholder="请输入密码"/>
    </div>

    <div class="form-group">
        <label>性别：</label>
        <input id="man" type="radio" name="sex" value="男" checked="checked"/>男
        &nbsp;&nbsp;&nbsp;
        <input id="women" type="radio" name="sex" value="女"/>女
    </div>

    <div class="form-group">
        <label for="age">年龄：</label>
        <input type="number" class="form-control" id="age" name="age" placeholder="请输入年龄"/>
    </div>

    <div class="form-group">
        <label for="address">籍贯：</label>
        <select name="address" id="address" class="form-control">
            <option value="北京">北京</option>
            <option value="上海">上海</option>
            <option value="广州">广州</option>
            <option value="深圳">深圳</option>
            <option value="成都">成都</option>
            <option value="杭州">杭州</option>
            <option value="重庆">重庆</option>
            <option value="西安">西安</option>
            <option value="武汉">武汉</option>
            <option value="沧州">沧州</option>
        </select>
    </div>

    <div class="form-group">
        <label for="qq">QQ：</label>
        <input type="text" id="qq" class="form-control" name="qq" placeholder="请输入QQ号码"/>
    </div>

    <div class="form-group">
        <label for="email">Email：</label>
        <input type="text" id="email" class="form-control" name="email" placeholder="请输入邮箱地址"/>
    </div>

    <div class="form-group">
        <label for="email">超级管理员：</label>
        <input id="admin_yes" type="radio" disabled="disabled" name="isadmin" value="是"/>是
        &nbsp;&nbsp;&nbsp;
        <input id="admin_no" type="radio" disabled="disabled" name="isadmin" value="否" checked="checked"/>否
    </div>

    <div class="form-group" style="text-align: center">
        <input id="btn_sub" class="btn btn-primary" type="button" value="提交" onclick="mysub()"/>
        <input id="btn_reset" class="btn btn-box" type="button" value="重置" onclick="location.href=location.href"/>
        <input id="btn_back" class="btn btn-default" type="button" value="返回" onclick="location.href='list.html'"/>
    </div>

</div>
<script>
    var uid = 0;
    // 查询某个 url 中的参数的值
    function getParamValue(key){
        var url = location.search;
        if(location.search != null){
            url = url.substring(1);
            var kvs = url.split("&");
            for(var i=0;i<kvs.length;i++){
                var kv = kvs[i].split("=");
                if(kv[0] == key) return kv[1];
            }
        }
        return "";
    }

    // 展示当前用户信息
    function loadUserInfo(){
        uid = getParamValue("uid");
        if(uid > 0){ // 当前id有效
             jQuery.ajax({
                 url:"/getuserbyid",
                 type:"POST",
                 data:{"uid":uid},
                 success:function (result){
                     if (result != null && result.data != null && result.data.uid >0){
                         // 得到的有效数据，对页面中的控件进行赋值
                         var userinfo = result.data;
                         jQuery("#loginname").val(userinfo.loginname);
                         jQuery("#username").val(userinfo.username);
                         var sex = userinfo.sex;
                         if(sex == "女"){
                             jQuery("#women").attr("checked","checked");
                         }
                         jQuery("#age").val(userinfo.age);
                         jQuery("#address").val(userinfo.address);
                         jQuery("#qq").val(userinfo.qq);
                         jQuery("#email").val(userinfo.email);
                         var isadmin = userinfo.isadmin;
                         if(isadmin){
                             jQuery("#admin_yes").attr("checked","checked");
                         }
                     }else {
                         alert("抱歉后端接口不可用，请稍后再试！")
                     }
                 }
             });
        }else {
            alert("非法参数");
        }
    }
    // 展示用户信息
    loadUserInfo();

    // 提交用户修改信息
    function mysub(){

        // 获取值
        var username = jQuery("#username");
        var password = jQuery("#password");
        var password2 = jQuery("#password2");
        var sex = jQuery("input[name=sex]:checked").val();
        var age = jQuery("#age").val();
        var address = jQuery("#address").val();
        var qq = jQuery("#qq").val();
        var email = jQuery("#email").val();
        var isadmin = jQuery("input[name=isadmin]:checked").val();

        // 非空校验
        if (jQuery.trim(username.val()) == "") {
            alert("请先输入昵称！");
            username.focus();
            return false;
        }
        if ( password.val() !="" || password2.val() !=""){
            if(password.val() != password2.val()){
                alert("两次输入的密码不一致，请检查！");
                password.focus();
                return false;
            }
        }

        // 提交数据到后端
        jQuery.ajax({
            url: "/update",
            type: "POST",
            data: {
                "uid":uid,
                "username": username.val(),
                "password": password.val(),
                "sex": sex,
                "age": age,
                "address": address,
                "qq": qq,
                "email": email
            }, success: function (result) {
                if (result != null && result.data > 0) {
                    alert("修改成功！");
                    location.href = "list.html";
                }else{
                    alert("修改失败，请刷新再试！");
                }
            }
        });
    }



</script>
</body>

</html>